/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.kimstore.gui.inventory;

import com.kimstore.gui.sales.NewOrderWindow;
import com.kimstore.main.controller.InventoryTabController;
import com.kimstore.bean.Product;
import com.kimstore.factory.DaoFactory;
import javax.swing.JFrame;

/**
 *
 * @author Administrator
 */
public class NewProductController {
    
    private InventoryTabController inventoryTabController;
    
    private NewProductWindow newProductWindow;
    
    public NewProductController(InventoryTabController inventoryTabController){
        this.inventoryTabController = inventoryTabController;
    }
    
    public void displayWindow(){
        newProductWindow = new NewProductWindow(this);
        newProductWindow.displayWindow();
    }
    
    public void fromWindow(Product product){
        inventoryTabController.createNewProductControllerCallback(product);
        DaoFactory.getInstance().getProductDao().saveOrUpdate(product);
    }
    
    public void validate(Product product) throws Error {
        if(product.getName().isEmpty() || product.getName() == null) {
            throw new Error("<html><b>Product Name</b> has not been filled up</html>");
        }
        
        boolean exists = DaoFactory.getInstance().getProductDao().existsName(product.getName());
        
        if(exists) {
            throw new Error("<html><b>Product Name</b> has already been taken");
        }
    }
    
}
